require 'crawler/crawler'
require 'uri'
require 'date'
require 'rexml/document'

module Crawler
  
  class YoutubeCrawler < Crawler
    
    public
    
    def initialize(args = {})
      if args[:stop_crawling?].nil? then
        args[:stop_crawling?] = lambda { |c| 
          doc = REXML::Document.new(c.body)
          return false if doc.root.nil?
          return false if doc.root.elements['error/code'].nil?
          ret = false
          doc.root.elements['error/code'].each { |e| ret = true if e.to_s.eql?("too_many_recent_calls") }
          ret
        }
      end
      
      super args
    end
    
    def service_name
      'youtube'
    end
    
    def crawl_table
      YoutubeCrawl  
    end
    
    protected
    
    def crawl(c)
      request_uri = "http://gdata.youtube.com/feeds/api/users/#{c.service.username_on_service}"
      @curl.url = request_uri
      @curl.http_get
      c.status = @curl.response_code
      response = @curl.response_code == 200
      c.body = @curl.body_str
      response
    end
    
    def has_crawl?(s)
      !s.youtube_crawl.nil?
    end
    
  end
end
